﻿using System;
using System.Data.Objects;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Model;

namespace NopSolutions.NopCommerce.BusinessLogic.Services
{
    public static class CustomerCompiledQueries
    {
        public static readonly Func<NopObjectContext, int, Customer> CustomerById = 
            CompiledQuery.Compile<NopObjectContext, int, Customer>(
                (ctx, id) => ctx.Customers.FirstOrDefault(customer => customer.CustomerId == id)
            );

        public static readonly Func<NopObjectContext, int, bool, IQueryable<CustomerRole>> CustomerRolesByCustomerId = 
            CompiledQuery.Compile<NopObjectContext, int, bool, IQueryable<CustomerRole>>(
                (ctx, id, hidden) => 
                        from cr in ctx.CustomerRoles
                        from c in cr.NpCustomers
                        where (hidden || cr.Active) &&
                            !cr.Deleted &&
                            c.CustomerId == id
                        orderby cr.Name descending
                        select cr
            );
    }
}
